# 按模块分组设置测试源文件
set(EXECUTION_TEST_SOURCES
    execution/test_order_manager.cpp
    execution/test_position_manager.cpp
    execution/execution_test.cpp
)

set(MARKET_DATA_TEST_SOURCES
    market_data/market_data_test.cpp
)

set(RISK_TEST_SOURCES
    risk/test_risk_manager.cpp
)

set(BACKTEST_TEST_SOURCES
    backtest/backtest_test.cpp
    backtest/ma_strategy_test.cpp
)

set(UTILS_TEST_SOURCES
    utils/json_utils_test.cpp
    utils/ring_buffer_test.cpp
    utils/object_pool_test.cpp
    utils/lockfree_queue_test.cpp
    utils/data_structures_benchmark.cpp
)

# 合并所有测试源文件
set(TEST_SOURCES
    ${CORE_TEST_SOURCES}
    ${MARKET_DATA_TEST_SOURCES}
    ${RISK_TEST_SOURCES}
    ${BACKTEST_TEST_SOURCES}
    ${UTILS_TEST_SOURCES}
    test_main.cpp
)

# 创建测试可执行文件
add_executable(unit_tests ${TEST_SOURCES})

# 设置包含目录
target_include_directories(unit_tests
    PRIVATE 
        ${CMAKE_SOURCE_DIR}/include
        ${CMAKE_BINARY_DIR}/include
        ${CMAKE_SOURCE_DIR}/src
        ${CMAKE_SOURCE_DIR}/tests
        ${CMAKE_SOURCE_DIR}/third_party
)

# 链接依赖库
target_link_libraries(unit_tests
    PRIVATE 
        hft_core
        gtest
        gtest_main
)

# 设置编译选项
if(MSVC)
    target_compile_options(unit_tests 
        PRIVATE 
            /W4 
            /MP
    )
    target_compile_definitions(unit_tests 
        PRIVATE
            _CRT_SECURE_NO_WARNINGS
            NOMINMAX
            WIN32_LEAN_AND_MEAN
    )
else()
    target_compile_options(unit_tests 
        PRIVATE 
            -Wall 
            -Wextra 
            -pedantic
    )
endif()

# 添加覆盖率支持
if(CMAKE_BUILD_TYPE STREQUAL "Coverage")
    target_compile_options(unit_tests PRIVATE --coverage)
    target_link_libraries(unit_tests PRIVATE --coverage)
endif()

# 添加测试
add_test(
    NAME unit_tests
    COMMAND unit_tests
)

# 设置测试属性
set_tests_properties(unit_tests
    PROPERTIES
        TIMEOUT 300  # 5分钟超时
        WILL_FAIL FALSE
)

# 添加自定义目标，用于运行所有测试
add_custom_target(run_tests
    COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure
    DEPENDS unit_tests
    COMMENT "Running all tests"
)

# 添加单独的测试目标
foreach(TEST_SOURCE ${TEST_SOURCES})
    # 从文件名中提取测试名称
    get_filename_component(TEST_NAME ${TEST_SOURCE} NAME_WE)
    
    # 如果不是主测试文件，则添加单独的测试目标
    if(NOT ${TEST_NAME} STREQUAL "test_main")
        add_executable(${TEST_NAME} ${TEST_SOURCE} test_main.cpp)
        
        target_include_directories(${TEST_NAME}
            PRIVATE 
                ${CMAKE_SOURCE_DIR}/include
                ${CMAKE_BINARY_DIR}/include
                ${CMAKE_SOURCE_DIR}/src
                ${CMAKE_SOURCE_DIR}/tests
                ${CMAKE_SOURCE_DIR}/third_party
        )
        
        target_link_libraries(${TEST_NAME}
            PRIVATE 
                hft_core
                gtest
                gtest_main
        )
        
        if(MSVC)
            target_compile_options(${TEST_NAME} PRIVATE /W4 /MP)
            target_compile_definitions(${TEST_NAME} PRIVATE _CRT_SECURE_NO_WARNINGS NOMINMAX WIN32_LEAN_AND_MEAN)
        else()
            target_compile_options(${TEST_NAME} PRIVATE -Wall -Wextra -pedantic)
        endif()
        
        add_test(
            NAME ${TEST_NAME}
            COMMAND ${TEST_NAME}
        )
        
        set_tests_properties(${TEST_NAME}
            PROPERTIES
                TIMEOUT 60  # 1分钟超时
                WILL_FAIL FALSE
        )
    endif()
endforeach()